%% The requirements on the OTP version come mainly from the patches
%% for OTP modules - see `otp_patches/` - requiring the version of
%% certain OTP applications, e.g. `mnesia`, to be well known in order
%% for such patches to be applied deterministically.
%%
%% The expected patched OTP modules required for the OTP release are
%% copied explicitly one by one as part of the `relx` overlay
%% configuration (in this `rebar.config`), in order to prevent
%% silently skipping patches e.g. because not built.
%%
%% If the user has a version of OTP that satisfies the specified
%% required minimum OTP version and that includes OTP applications
%% with version different (e.g. greater) than the well known ones
%% (e.g. because of a new OTP version with a higher-versioned OTP
%% application e.g. `mnesia`), then the patched OTP modules are not
%% built (!) though the explicit copying in the `relx` overlay fails.
%% Addressing such a failure consists of updating the content of
%% `otp_patches/`.
{minimum_otp_vsn, "24.3.4.15"}.

{project_app_dirs, ["apps/*"]}.

{erl_opts, [debug_info, {parse_transform, lager_transform},
            {lager_extra_sinks, [epoch_mining,
                                 epoch_metrics,
                                 epoch_sync,
                                 ae_db_gc,
                                 aestratum]}]}.

%% NOTE: When possible deps are referenced by Git ref to ensure consistency between builds.
{deps, [
        %% off-the-shelf dependencies

        {enoise, {git, "https://github.com/aeternity/enoise.git",
                 {ref, "8acbce9"}}},

        {setup, "2.1.1"},
        {gproc, "0.9.0"},
        {jobs, "0.9.0"},
        {exometer_core, "2.0.0"},
        {trace_runner, "1.0.0"},
        {yamerl, "0.7.0"},
        {sext, "1.8.0"},
        {argparse, {git, "https://github.com/aeternity/argparse", {ref, "a3d8aa0"}}},
        {redbug, {git, "https://github.com/massemanet/redbug.git", {ref, "325cb63"}}},
        {kache, {git, "https://github.com/klarna-incubator/kache.git", {ref, "e5e10c6"}}},

        {lager, {git, "https://github.com/aeternity/lager.git",
                {ref, "a119ed7"}}},
        {app_ctrl, {git, "https://github.com/aeternity/app_ctrl.git", {ref, "c1fb927"}}},

        {cowboy, {git, "https://github.com/ninenines/cowboy.git",
                 {tag, "2.11.0"}}},
        {idna, {git, "https://github.com/benoitc/erlang-idna",
               {ref, "6cff727"}}}, % tag: 6.0.0
        {nat, {git, "https://github.com/aeternity/erlang-nat.git",
              {ref, "da8b260"}}},
        {jsx, {git, "https://github.com/talentdeficit/jsx.git",
              {ref, "3074d48"}}},
        {lz4, {git, "https://github.com/szktty/erlang-lz4.git",
              {ref, "9ea04f2"}}},

        %% deps originally from aeternity

        % The rocksdb dependencies are removed on win32 to reduce build times,
        % because they are currently not working on win32.
        {mnesia_rocksdb, {git, "https://github.com/aeternity/mnesia_rocksdb.git",
                         {ref, "9cea41c"}}},

        {aeminer, {git, "https://github.com/aeternity/aeminer.git",
                  {ref, "c6b6a6d"}}},

        {aebytecode, {git, "https://github.com/aeternity/aebytecode.git",
                     {tag, "v3.4.0"}}},

        {aeserialization, {git, "https://github.com/aeternity/aeserialization.git",
                          {ref, "a1204df"}}},

        {aestratum_lib, {git, "https://github.com/aeternity/aestratum_lib.git",
                        {ref, "17b56c5"}}},

        {ecrecover, {git, "https://github.com/aeternity/ecrecover.git",
                    {ref, "ce4175e"}}},

        {emcl, {git, "https://github.com/aeternity/emcl.git",
               {ref, "e988f69"}}},

        %% forks

        {enacl, {git, "https://github.com/aeternity/enacl.git",
                {ref, "4eb7ec7"}}},

        {jesse, {git, "https://github.com/for-GET/jesse.git",
                {ref, "cf075d2"}}},

        % upstream is not maintained anymore
        {base58, {git, "https://github.com/aeternity/erl-base58.git",
                 {ref,"60a3356"}}},

        % upstream is not maintained anymore
        {sha3, {git, "https://github.com/aeternity/erlang-sha3",
               {ref, "b5f27a2"}}},

        % for the online telecom style CLI
        {ecli, {git, "https://github.com/mgmtd/ecli",
          {ref, "d9f871e"}}}

       ]}.

{plugins, [
           {swagger_endpoints, {git, "https://github.com/aeternity/swagger_endpoints", {ref, "dcbfb26"}}}
          ]}.

{swagger_endpoints, [{src, "apps/aehttp/priv/swagger.yaml"}, {dst, "apps/aeutils/src/endpoints.erl"}]}.

{relx, [{release, { aeternity, "version value comes from VERSION" },
         % sasl is required for the command `aeternity versions` to work,
         % it is disabled in `sys.config` though. Note that setup must be
         % started before lager
         [runtime_tools, sasl, setup, lager, sext, gproc, jobs, lz4, argparse, trace_runner, app_ctrl,
          {rocksdb, load}, {mnesia_rocksdb, load}, {mnesia, load}, kache,
          parse_trans, exometer_core, ranch, aeminer, aecore, aeapi, aehttp, enacl, enoise,
          aebytecode, aeserialization, aevm, aechannel, aefate, aemon, aestratum, ecrecover,
          aesync, ecli, aedevmode]},

        %% Make sure wx doesn't get included - from OTP-27 it is listed
        %% as an optional dependency for observer.
        {exclude_apps, [wx]},

        {sys_config, "./config/sys.config"},
        {vm_args, "./config/vm.args"},

        {dev_mode, true},
        {include_erts, false},

        {overlay_vars, "scripts/extensions.vars"},
        {overlay, [{copy, "REVISION", "REVISION"},
                   {copy, "VERSION" , "VERSION"},
                   {copy, "docs/build.md", "docs/build.md"},
                   {copy, "docs/installation.md" , "docs/installation.md"},
                   {copy, "docs/configuration.md" , "docs/configuration.md"},
                   {copy, "docs/cuda-miner.md", "docs/cuda-miner.md"},
                   {copy, "docs/operation.md" , "docs/operation.md"},
                   {copy, "docs/monitoring.md" , "docs/monitoring.md"},
                   {copy, "docs/release-notes" , "docs/release-notes"},
                   {copy, "plugins" , "plugins"},
                   {template, "./config/launch", "launch"},
                   {mkdir, "data/aecore/.genesis"},
                   {copy, "data/aecore/.genesis/accounts.json", "data/aecore/.genesis/accounts.json"},
                   {copy, "data/aecore/.genesis/accounts_uat.json", "data/aecore/.genesis/accounts_uat.json"},
                   {copy, "data/aecore/.genesis/accounts_test.json", "data/aecore/.genesis/accounts_test.json"},
                   {copy, "data/aecore/.genesis/messages.json", "data/aecore/.genesis/messages.json"},
                   {mkdir, "data/aecore/.minerva"},
                   {copy, "data/aecore/.minerva/accounts.json", "data/aecore/.minerva/accounts.json"},
                   {copy, "data/aecore/.minerva/accounts_uat.json", "data/aecore/.minerva/accounts_uat.json"},
                   {copy, "data/aecore/.minerva/accounts_test.json", "data/aecore/.minerva/accounts_test.json"},
                   {mkdir, "data/aecore/.fortuna"},
                   {copy, "data/aecore/.fortuna/accounts.json", "data/aecore/.fortuna/accounts.json"},
                   {copy, "data/aecore/.fortuna/accounts_uat.json", "data/aecore/.fortuna/accounts_uat.json"},
                   {copy, "data/aecore/.fortuna/accounts_test.json", "data/aecore/.fortuna/accounts_test.json"},
                   {mkdir, "data/aecore/.lima"},
                   {copy, "data/aecore/.lima/accounts.json", "data/aecore/.lima/accounts.json"},
                   {copy, "data/aecore/.lima/accounts_uat.json", "data/aecore/.lima/accounts_uat.json"},
                   {copy, "data/aecore/.lima/accounts_test.json", "data/aecore/.lima/accounts_test.json"},
                   {copy, "data/aecore/.lima/extra_accounts.json", "data/aecore/.lima/extra_accounts.json"},
                   {copy, "data/aecore/.lima/extra_accounts_uat.json", "data/aecore/.lima/extra_accounts_uat.json"},
                   {copy, "data/aecore/.lima/extra_accounts_test.json", "data/aecore/.lima/extra_accounts_test.json"},
                   {copy, "data/aecore/.lima/contracts.json", "data/aecore/.lima/contracts.json"},
                   {copy, "data/aecore/.lima/contracts_uat.json", "data/aecore/.lima/contracts_uat.json"},
                   {copy, "data/aecore/.lima/contracts_test.json", "data/aecore/.lima/contracts_test.json"},
                   {mkdir, "data/aecore/.ceres"},
                   {copy, "data/aecore/.block_whitelist.json", "data/aecore/.block_whitelist.json"},
                   {copy, "data/aecore/.pre_iris_map_ordering.json", "data/aecore/.pre_iris_map_ordering.json"},
                   {copy, "data/aecore/.pre_iris_map_ordering_uat.json", "data/aecore/.pre_iris_map_ordering_uat.json"},
                   {template, "scripts/aeternity_bin", "bin/aeternity"},
                   {copy, "scripts/nodetool", "bin/nodetool"},
                   {copy, "hooks/pre_start.sh", "bin/hooks/pre_start.sh"},
                   {copy, "apps/check_config/priv/extensions/check_config.sh", "bin/extensions/check_config"},
                   {copy, "apps/aeutils/priv/extensions/keys_gen.sh", "bin/extensions/keys_gen"},
                   {copy, "apps/aeutils/priv/extensions/get_peer_key.sh", "bin/extensions/peer_key"},
                   {copy, "apps/aeutils/priv/extensions/create_whitelist.sh", "bin/extensions/create_whitelist"},
                   {copy, "apps/aeutils/priv/extensions/db_rollback.sh", "bin/extensions/db_rollback"},
                   {copy, "apps/aeutils/priv/extensions/db_migrate.sh", "bin/extensions/db_migrate"},
                   {copy, "apps/aeutils/priv/extensions/db_safe_access_scan.sh", "bin/extensions/db_safe_access_scan"},
                   {copy, "apps/aeutils/priv/extensions/maintenance.sh", "bin/extensions/maintenance"},
                   {copy, "apps/aeutils/priv/extensions/offline.sh", "bin/extensions/offline"},
                   {copy, "apps/aeutils/priv/extensions/admin.sh", "bin/extensions/admin"},
                   {copy, "apps/aeutils/priv/extensions/messages_hash.sh", "bin/extensions/messages_hash"},
                   {copy, "apps/aeutils/priv/extensions/cli.sh", "bin/extensions/cli"},
                   {copy, "apps/aeutils/priv/extensions/status.sh", "bin/extensions/status"},
                   {copy, "apps/aeutils/priv/extensions/version.sh", "bin/extensions/version"}
                  ]},
        {generate_start_script, false}
        ]}.

{profiles, [{local, [{relx, [{dev_mode, true},
                             {include_erts, false},
                             {include_src, true}]}]
            },
            {dev1, [{relx, [{dev_mode, false},
                            {include_erts, false},
                            {sys_config, "./config/dev1/sys.config"},
                            {vm_args, "./config/dev1/vm.args"},
                            {overlay, [{mkdir, "{{output_dir}}/data"},
                                       {copy,
                                        "_build/dev1/bin/check_config",
                                        "{{output_dir}}/bin/check_config"},
                                       {copy,
                                       "_build/dev1/lib/aeutils/priv/aeternity_config_schema.json",
                                       "{{output_dir}}/data/aeternity_config_schema.json"}]}
                           ]},
                     {erl_opts, [{d, 'TEST'}, {d, 'DEBUG_INFO'}]}
                    ]
            },
            {test, [{relx, [{dev_mode, true},
                            {include_erts, false},
                            {include_src, true},
                            {sys_config, "./config/dev1/sys.config"},
                            {vm_args, "./config/dev1/vm.args"}]},
                    {erl_opts, [{d, 'DEBUG_INFO'}]},
                    {dist_node, [{setcookie, 'aeternity_cookie'},
                                 {sname, 'aeternity_ct@localhost'}]},
                    {deps, [{meck, "0.8.12"},
                            {aesophia, {git, "https://github.com/aeternity/aesophia.git", {ref,"ffdd4ec"}}},
                            {aesophia_cli, {git, "https://github.com/aeternity/aesophia_cli", {ref,"e0a1730"}}},
                            {aestratum_client, {git, "https://github.com/aeternity/aestratum_client", {ref, "cfd406a"}}},
                            {websocket_client, {git, "https://github.com/aeternity/websocket_client", {ref, "506d8e2"}}},
                            {aesophia_aci_encoder, {git, "https://github.com/aeternity/aesophia_aci_encoder", {ref, "1f55ee5"}}}
                           ]}
                   ]},
            {prod, [{relx, [{dev_mode, false},
                            {include_erts, true},
                            {include_src, false},
                            {overlay, [{copy,
                                        "_build/prod/bin/check_config",
                                        "bin/check_config"},
                                       {copy,
                                        "_build/prod/lib/aeutils/priv/aeternity_config_schema.json",
                                        "data/aeternity_config_schema.json"}]}
                           ]}
                   ]},
            {system_test, [
                {extra_src_dirs, ["system_test/common", "system_test/common/helpers", "apps/aehttp/test", "apps/aecontract/test/include"]},
                {shell, [{apps, []}]},
                {deps, [
                    bbmustache,
                    {hackney, "1.14.3"},
                    {websocket_client, ".*", {git, "https://github.com/aeternity/websocket_client", {ref, "95ef9de"}}}
                ]},
                {ct_opts, [{create_priv_dir, auto_per_tc}]}
            ]},
            {eqc, [{project_app_dirs, ["eqc_test"]},
                   {extra_src_dirs, [ "eqc/aecore_eqc"
                                    , "eqc/aega_eqc"
                                    , "eqc/aeminer_eqc"
                                    , "eqc/aesophia_eqc"
                                    , "eqc/aechannel_eqc"
                                    , "eqc/aeutils_eqc"
                                    ]}]}
           ]
}.

{escript_main_app, check_config}.
{escript_name, "check_config"}.
{escript_incl_apps, [aeutils, jsx, yamerl, jesse, rfc3339]}.
{escript_shebang, "#!/usr/bin/env escript\n"}.
{escript_comment, "%%\n"}.

{overrides, [
              {add, bear, [{erl_opts, [nowarn_match_float_zero]}]}
            ]}.


{provider_hooks,
 [{pre, []},
  {post, [{compile, escriptize}]}]}.

{pre_hooks, [
	     {compile, "make -C ./otp_patches all"},
             {compile, "erlc test/ct_eunit_xform.erl"} %% {ct_first_files, _} does not work
            ]}.

{post_hooks, [{compile, "rm -f ct_eunit_xform.beam"},
              {clean, "make -C ./otp_patches clean"}
             ]}.

{ct_opts, [
  {ct_hooks, [{cth_surefire, [{path, "../junit.xml"}]},
              {aect_cache_restorer, []}]}
]}.

{eunit_opts, [no_tty,
              verbose,
              {report, {eunit_progress, [colored, profile]}},
              {report, {eunit_surefire, [{dir, "eunit_report/"}]}}
             ]}.

{dialyzer, [
            {warnings, [unknown]},
            {plt_apps, all_deps},
            {base_plt_apps, [erts, kernel, stdlib, crypto, mnesia, trace_runner, rocksdb, mnesia_rocksdb, app_ctrl]}
           ]}.

{edoc_opts, [{preprocess, true}]}.
